<?php
namespace app\users\controller;
use app\common\controller\HomeCommon;
use think\Request;
use think\Validate;



/**
 * 1、发布活动
 * 1、加入活动
 * @author admin
 *
 */
class Activity extends HomeCommon
{
	public function __construct(){
		parent::__construct();
		$token=input('token');
		//验证token
		$result=$this->check_token($token);
		
		if($result['errcode']!=0){
			
			echo json_encode(['errcode'=>'10001','msg'=>'token错误！','data'=>[]]);die;
		}
	}
	/**
	 * 1、发布活动
	 */
	public  function publish(){
		if(Request::instance()->isPost()){
			
			
			$validate = new Validate([
					'title'  => 'require|length:1,20',
					'desc' => 'require|length:1,100',
					'address' => 'require',
					'lat' => 'require',
					'lng' => 'require',
					'time' => 'require',
					'limit_num' => 'require',
					//'park_id' => 'require',
			]);
			$data = [
					'title'  => empty(input('activity_id'))?0:input('images'),
					'images' => empty(input('activity_id'))?0:input('images'),
					'address' => empty(input('activity_id'))?0:input('address'),
					'desc' => empty(input('activity_id'))?0:input('desc'),
					'lat' => empty(input('activity_id'))?0:input('lat'),
					'lng' => empty(input('activity_id'))?0:input('lng'),
					'time' => empty(input('activity_id'))?0:input('time'),
					'limit_num' => empty(input('limit_num'))?1:input('limit_num'),
					'park_id' => empty(input('park_id'))?'':input('park_id'),
			];
			if (!$validate->check($data)) {
				return json(['errcode'=>'10001','msg'=>$validate->getError(),'data'=>[]]);
			}
			$data['user_id']=$this->user_id;
			if(db('activity')->insert($data)){
				return json(['errcode'=>'0','msg'=>'添加成功','data'=>['insertid'=>db('activity')->getLastInsID()]]);
			}else{
				return json(['errcode'=>'0','msg'=>'添加失败','data'=>[]]);
			}
			
			
		}
	}
	/**
	 *2、加入活动
	 */
	public  function join_activity(){
		if(Request::instance()->isPost()){
			$data = [
					'activity_id'  => empty(input('activity_id'))?0:input('activity_id'),
					'user_id' => $this->user_id,
				
			];
			$ac_info=db('activity')->field('limit_num,time')
			->where(['status'=>1])->find($data['activity_id']);
			//活动不存在
	        if(empty($ac_info)){
	        	return json(['errcode'=>'10001','msg'=>'活动不存在，或者已过期','data'=>[]]);
	        }
			 //报名人数已经满
	        $total_num=db('activity_join_users')->where(['activity_id'=>$data['activity_id'],'status'=>1])->count('id');
	        if($total_num>=$ac_info['limit_num']){
	        	return json(['errcode'=>'10002','msg'=>'活动人数已经满额','data'=>[]]);
	        }
			 //活动已经超时
			 
	        //已经参与过
	        $his=db('activity_join_users')->where($data)->find();
	        if(!empty($his)){
	        	return json(['errcode'=>'10003','msg'=>'你已经报过名','data'=>[]]);
	        }
			 
	        if(db('activity_join_users')->insert($data)){
	        	return json(['errcode'=>'0','msg'=>'加入成功，等待审核','data'=>['insertid'=>db('activity_join_users')->getLastInsID()]]);
	        }else{
	        	return json(['errcode'=>'0','msg'=>'加入失败','data'=>[]]);
	        }
	        
			 
		}
	}
}